package com.bcxj.apiboss.controller;

import com.bcxj.apiboss.service.AdminUserService;
import com.bcxj.internalcommon.dto.AdminUserDTO;
import com.bcxj.internalcommon.entity.AdminUser;
import com.bcxj.internalcommon.response.ResponseResult;
import com.bcxj.internalcommon.response.TokenResult;
import com.bcxj.internalcommon.util.JwtUtils;
import com.bcxj.internalcommon.vo.AdminUserVO;
import feign.Response;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.http.HttpRequest;

/**
 * ClassName: AdminController
 * Package: com.bcxj.apiboss.controller
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/4/26 10:22
 * @Version 1.0
 * @Since 1.0
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {


    @Autowired
    private AdminUserService service;

    /**
     * 管理员登录
     * @param userDTO
     * @return
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody AdminUserDTO userDTO) throws Exception {
        System.out.println(userDTO);
        return service.adminLogin(userDTO.getAccount(), userDTO.getPassword());
    }


    /**
     * 获取管理员信息
     * @param request
     * @return
     */
    @GetMapping("getinfo")
    public ResponseResult getAdmin(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        TokenResult tokenResult = JwtUtils.parseToken(token);

        return service.getAdmin(tokenResult.getPhone());
    }


    /**
     * 增加用户
     * @param adminUser
     * @return
     */
    @PostMapping("add")
    public ResponseResult addAdmin(@RequestBody AdminUser adminUser) throws Exception {
        return service.addAdmin(adminUser);
    }


    /**
     * 获取全部用户的信息
     * @return
     */
    @GetMapping("all")
    public ResponseResult getAll() {
        return service.getAll();
    }


    /**
     * 更新用户的状态
     * @return
     */
    @PostMapping("update")
    public ResponseResult updateAdmin(AdminUserVO adminUserVO) {
        return service.updateAdmin(adminUserVO);
    }
}
